#pragma once 

#include <iostream>
#include <pthread.h>
#include <vector>
#include <string>
#include <queue>
#include "Task.hpp"


using namespace std;
struct ThreadInfo
{
    pthread_t tid;
    string name;
};
static const int defalutnum = 5;

template<class T>
class ThreadPool
{
private:
    void Lock()
    {
        pthread_mutex_lock(&mutex_);
    }
    void Unlock()
    {
        pthread_mutex_unlock(&mutex_);
    }
    void Wakeup()
    {
        pthread_cond_signal(&cond_);
    }
    void ThreadSleep()
    {
        pthread_cond_wait(&cond_, &mutex_);
    }
    bool IsQueueEmpty()
    {
        return task_s_.empty();
    }
    std::string GetThreadName(pthread_t tid)
    {
        for(const auto& ti : thread_s_)
        {
            if(ti.tid == tid)
            {
                return ti.name;
            }
        }
        return "None";
    }
public:

    static void* HandlerTask(void* args)
    {
        ThreadPool<T> * tp = static_cast<ThreadPool*>(args);
        std::string name = tp->GetThreadName(pthread_self());
        while(true)
        {
            tp->Lock();
            while(tp->IsQueueEmpty())
            {
                tp->ThreadSleep();
            }
            T t = tp->Pop();
            tp->Unlock();            
            t();
            std::cout << name << " run " << "result: " << t.GetResult() << std::endl;
        }
    }

    void Push(const T & t)
    {
        Lock();
        task_s_.push(t);
        Wakeup();
        Unlock();
    }   
    T Pop()
    {
        T t = task_s_.front();
        task_s_.pop();
        return t;
    }
    void Start()
    {
        int num = thread_s_.size();
        for(int i = 0; i < num; i++)
        {
            thread_s_[i].name = "thread-" + to_string(i + 1);
            pthread_create(&thread_s_[i].tid, nullptr, HandlerTask, this);
        }
    }
    static ThreadPool<T>* GetInstance()
    {
        if(tp_ == nullptr)
        {
            pthread_mutex_lock(&lock_);
            if(tp_ == nullptr)
            {
                cout << "log: singleton create done first ..."  << endl;
                tp_ = new ThreadPool<T>(); 
            }
            pthread_mutex_unlock(&lock_);
        }
        return tp_;
    }
private:
    //懒汉模式要把默认生成的构造，拷贝构造，赋值运算符重载给删掉
    ThreadPool(int num = defalutnum)
        :thread_s_(num)
    {
        pthread_mutex_init(&mutex_, nullptr);
        pthread_cond_init(&cond_, nullptr);
    }
    ~ThreadPool()
    {
        pthread_mutex_destroy(&mutex_);
        pthread_cond_destroy(&cond_);
    }
    ThreadPool(const ThreadPool<T>& tp) = delete;
    const ThreadPool<T>& operator=(const ThreadPool<T>& tp) = delete;
private:
    vector<ThreadInfo> thread_s_;
    queue<T> task_s_;

    pthread_mutex_t mutex_;
    pthread_cond_t cond_;

    static ThreadPool<T>* tp_;
    static pthread_mutex_t lock_;
};
//类外初始化
template<class T>
ThreadPool<T>* ThreadPool<T>::tp_ = nullptr;
template<class T>
pthread_mutex_t ThreadPool<T>::lock_ = PTHREAD_MUTEX_INITIALIZER;